import { ref, reactive } from 'vue';
import { message } from 'ant-design-vue';
import {saveProcessDraft, startProcess,userApprove,
        userDiscard,userReject,userTransfer,userRecall,
        userSignature,userAppend,userCirculate,userReminder,
        getElementButton, saveFormDataJson, 
       } from '@/api/flow/review';
import { t } from '@/hooks/useI18n';
import { userSaveWorkflowDraftRequest,userApproveRequest,
         userDiscardRequest,userRejectRequest,userSignatureRequest 
} from './userApproveRequest';
import { isReturnStatement } from 'typescript';
import { append } from 'tiny-svg';

export const useApproveToolbar = (
  previewRef,
  emit,
  loading,
  previewConfig: any,
  bussid,
  errorMsg,
  msgModalOpen,
) => {
  const toolBar = reactive({
    end: false,
    transfer: false,
    countersign: false,
    refuse: false,
    returnWork: 'none',
    processStart: false,
    sendWork: true,
    ccWay: 'none',
    autoCCWay: 'none',
    task_def_key: '',
  });

  const searchForm = reactive({
    bmsType: '',
    task_def_id: '',
  });

  const approval = reactive({
    businessKey: '',
    processInstanceId: '',
    taskId: '',
    variables: {
      expType: '',
      nextNode: '',
    },
  });

  const nextNodes = ref([]);
  
  var useroperation= {
    draft: function (request) {    
      saveWorkflowdraft(request);
    }, 
    start: function(request)
    {
      processStart(request);
    },
    approve: function(request)
    {
      userApproveOperation(request);
    },
    reject: function(request)
    {
      userRejectOperation(request);
    },
    transfer:function(request)
    {
      userTransferOperation(request);
    },
    reminder:function(request)
    {
      userReminderOperation(request);
    },
    discard:function(request)
    {
      userDiscardOperation(request);
    },
    recall:function(request)
    {
      userRecallOperation(request);
    },
    signature:function(request)
    {
      userSupplementSignatureOperation(request);
    },
    circulate:function(request)
    {
      userCirculateOperation(request);
    },
    append:function(request)
    {
      userAppendOperation(request);
    },
  };
  
  var actionClick = async function (key, request) {
    return useroperation[key](request);
  };

  // 驳回
  const showReturnModal = async () => {};

  // 提交

  const keyFuncs = {
    byManuallySelectDept: (result) => {
      // this.getNextUsers();
      // this.dialogSelectUser=true;
    },
    byManuallySelectRole: (result) => {
      // this.getNextUsers();
      // this.dialogSelectUser=true;
    },
    allUser: () => {
      // this.dialogAllUser=true;
    },
    byManuallySelectStion: (result) => {
      // this.getNextUsers();
      // this.dialogSelectUser=true;
    },
    bySelect: (result) => {
      approval.variables.expType = result.params;
      nextNodes.value = result.task_key;
      // this.dialogSelectNode=true;
    },
  };

  const defaultKeyFunc = (key, result) => {
    !['allUser', 'bySelect'].includes(key) && (searchForm.bmsType = key);
    key != 'bySelect' && (searchForm.task_def_id = result.task_key);
    approval.businessKey = bussid.value;
    approval.processInstanceId = result.proc_inst_id;
    approval.taskId = result.taskId;
  };

  

 
  // 暂存草稿，保存表单数据以及启动流程实例
  const saveWorkflowdraft = async (request) => {           
    try {        
    loading.value = true;  
    const res = await saveProcessDraft(request);    
    if (res && res.status == '200') {      
      message.success(res.result.msg);
      loading.value = false;
      emit('cancel');
      return true;
      
    } else {
      message.error('保存失败');
      loading.value = false;
      return res;
    }
  } catch (e) {
    loading.value = false;
    return false;
  }



  };
  //启动流程
  const processStart = async (request) => {
    loading.value = true;
    try {      
      const result = await startProcess(request);      
      if (result && result.status == '200') {
        message.success('申请已提交。');
        loading.value = false;
        emit('cancel');        
      } else {
        message.warning(result.result.msg);
        //todo:出现错误的反应
        //errorMsg.resType = 'error';
        //errorMsg.resTypeMsg = result.error == '500' ? result.msg : result.result.msg;
        //msgModalOpen.value = true;
      }
      
    } catch (e) {}
    loading.value = false;
  };
//通过操作
  const userApproveOperation = async(request) =>{    
    try {      
      const res = await userApprove(request);  
      if (res && res.status == '200') {
        bussid.value = res.result;
        message.success('审批已通过');
        loading.value = false;
        emit('cancel');
        return true;
      } else {
        message.error('审批通过失败');
        loading.value = false;
        return res;
      }
    } catch (e) {
      loading.value = false;
      return false;
    }
  };

//驳回操作
const userRejectOperation = async(request) =>{    
  try {      
    const res = await userReject(request);  
    if (res && res.status == '200') {
      bussid.value = res.result;
      message.success('驳回成功');
      loading.value = false;
      emit('cancel');
      return true;
    } else {
      message.error('驳回失败');
      loading.value = false;
      return res;
    }
  } catch (e) {
    loading.value = false;
    return false;
  }
};

//转办
const userTransferOperation = async(request) =>{    
  try {      
    const res = await userTransfer(request);  
    if (res && res.status == '200') {
      bussid.value = res.result;
      message.success('审批已转办');
      loading.value = false;
      emit('cancel');
      return true;
    } else {
      message.error('转办失败');
      loading.value = false;
      return res;
    }
  } catch (e) {
    loading.value = false;
    return false;
  }
};


//废弃操作
  const userDiscardOperation = async(request) =>{    
    try {
      
      const res = await userDiscard(request);  
      if (res && res.status == '200') {
        bussid.value = res.result;
        message.success('废弃成功');
        loading.value = false;
        emit('cancel');
        return true;
      } else {
        message.error('废弃失败');
        loading.value = false;
        return res;
      }
    } catch (e) {
      loading.value = false;
      return false;
    }
  };
//撤回操作
const userRecallOperation = async(request) =>{    
  try {
    
    const res = await userRecall(request);  
    if (res && res.status == '200') {
      bussid.value = res.result;
      message.success('撤回成功');
      loading.value = false;
      emit('cancel');
      return true;
    } else {
      message.error('撤回失败');
      loading.value = false;
      return res;
    }
  } catch (e) {
    loading.value = false;
    return false;
  }
};
//加签操作
const userSupplementSignatureOperation = async(request) =>{    
  try {
    
    const res = await userSignature(request);  
    if (res && res.status == '200') {
      bussid.value = res.result;
      message.success('加签成功');
      loading.value = false;
      emit('cancel');
      return true;
    } else {
      message.error('加签失败');
      loading.value = false;
      return res;
    }
  } catch (e) {
    loading.value = false;
    return false;
  }
};
//传阅操作
const userCirculateOperation = async(request) =>{    
  try {
    
    const res = await userCirculate(request);  
    if (res && res.status == '200') {
      bussid.value = res.result;
      message.success('传阅成功');
      loading.value = false;
      emit('cancel');
      return true;
    } else {
      message.error('传阅失败');
      loading.value = false;
      return res;
    }
  } catch (e) {
    loading.value = false;
    return false;
  }
};

//催办操作
const userReminderOperation = async(request) =>{    
  try {    
    const res = await userReminder(request);  
    if (res && res.status == '200') {
      bussid.value = res.result;
      message.success('催办成功');
      loading.value = false;
      emit('cancel');
      return true;
    } else {
      message.error('催办失败');
      loading.value = false;
      return res;
    }
  } catch (e) {
    loading.value = false;
    return false;
  }
};


const userAppendOperation = async(request) =>{    
  try {    
    const res = await userAppend(request);  
    if (res && res.status == '200') {
      bussid.value = res.result;
      message.success('操作成功');
      loading.value = false;
      emit('cancel');
      return true;
    } else {
      message.error('操作失败');
      loading.value = false;
      return res;
    }
  } catch (e) {
    loading.value = false;
    return false;
  }
};


  // 同意
  const sendWork = async () => {
   

  };
  // 关闭
  const closeDialog = async () => {
    emit('cancel');
  };
  const initToolBar = async (previewConfig) => {
  
    if (
      previewConfig.procinstid == undefined ||
      previewConfig.procinstid == '' ||
      previewConfig.procinstid == null
    ) {
      toolBar.processStart = true;
      toolBar.sendWork = false;
      return;
    }

   //request 里面决定了，所有的get方法的参数必须使用 params命名
   /*
    getElementButton({             
      taskid: previewConfig.taskid,    
    }).then((res) => {
      if (res.error == '200') {
        toolBar.transfer = res.result.transfer == 1;
        toolBar.returnWork = res.result.returnWay;
        toolBar.countersign = res.result.countersign == 1;
        toolBar.end = res.result.endTask == 1;
        toolBar.refuse = res.result.refuse == 1;
        toolBar.ccWay = res.result.ccWay;
        toolBar.autoCCWay = res.result.autoCCWay;
        toolBar.task_def_key = res.result.task_def_key;
      } else {
        message.warning(res.error);
      }
    });
    */
    
  };
  return {
    toolBar,
    actionClick,
    showReturnModal,
    processStart,
    saveWorkflowdraft,
    sendWork,
    closeDialog,
    initToolBar,
    errorMsg,
  };
};
